package com.s7.statusbar;

import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.s7.statusbar.compat.KitkatCompat;
import com.s7.statusbar.compat.LollipopCompat;
import com.s7.statusbar.compat.LowerCompat;
import com.s7.statusbar.compat.MCompat;
import com.s7.statusbar.impl.StatusBar;
import com.s7.statusbar.type.EMUI;
import com.s7.statusbar.type.MIUI;

public class StatusBarCompat {

    static final StatusBar COMPAT;
    static {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            COMPAT = new MCompat();
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !EMUI.isEMUI()) {
            COMPAT = new LollipopCompat();
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            COMPAT = new KitkatCompat();
        } else {
            COMPAT = new LowerCompat();
        }
    }

    /**
     * 设置状态栏透明
     * @param activity
     * @param translucent
     */
    public static void translucentStatusBar(@NonNull Activity activity, boolean translucent) {
        translucentStatusBar(activity.getWindow(), translucent);
    }

    /**
     * 设置状态栏透明
     * @param window
     * @param translucent
     */
    public static void translucentStatusBar(Window window, boolean translucent) {
        COMPAT.translucentStatusBar(window, translucent);
    }

    /**
     * 设置状态栏颜色
     * @param activity 需要设置的activity
     * @param color 颜色16进制（0xFFFFFFFF）
     */
    public static void setStatusBarColor(@NonNull Activity activity, @ColorInt int color) {
        setStatusBarColor(activity.getWindow(), color);
    }

    /**
     * 设置状态栏颜色
     * @param window 需要设置的activity
     * @param color 颜色16进制（0xFFFFFFFF）
     */
    public static void setStatusBarColor(Window window, @ColorInt int color) {
        setStatusBarColor(window, color, ColorManager.toGrey(color) > 225);
    }

    /**
     * 设置状态栏颜色
     * @param activity 需要设置的activity
     * @param color 颜色16进制（0xFFFFFFFF）
     * @param lightStatusBar 是否设置浅色调. 仅在部分设备中有效.
     */
    public static void setStatusBarColor(Activity activity, @ColorInt int color, boolean lightStatusBar) {
        setStatusBarColor(activity.getWindow(), color, lightStatusBar);
    }

    /**
     * 设置状态栏颜色
     * @param window  需要设置的 window
     * @param color   颜色16进制（0xFFFFFFFF）
     * @param lightStatusBar 是否设置浅色调. 仅在部分设备中有效.
     */
    public static void setStatusBarColor(Window window, @ColorInt int color, boolean lightStatusBar) {
        if ((window.getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) > 0
                || StatusBarExclude.exclude) {
            return;
        }
        COMPAT.setStatusBarColor(window, color);
        MIUI.setLightStatusBar(window, lightStatusBar);
    }

    /**
     * 标题栏渐变(Toolbar)
     * @param activity
     * @param toolbar
     * @param color
     * @param alpha
     */
    public static void setToolbarColor(Activity activity, View toolbar, int color, int alpha) {
        setToolbarColor(activity.getWindow(), toolbar, color, alpha);
    }

    /**
     * 标题栏渐变(Toolbar)
     * @param window
     * @param toolbar
     * @param color
     * @param alpha
     */
    public static void setToolbarColor(Window window, View toolbar, int color, int alpha) {
        COMPAT.translucentStatusBar(window, true);
        COMPAT.setToolbarColor(window, toolbar, color, alpha);
    }

    /**
     * 复杂交互状态栏变化(CollapsingToolbarLayout + Toolbar)
     * @param activity
     * @param bar
     * @param collapsing
     * @param toolbar
     * @param color
     */
    public static void setCollapsingToolbar(Activity activity, AppBarLayout bar, CollapsingToolbarLayout collapsing,
                                            Toolbar toolbar, int color) {
        setCollapsingToolbar(activity.getWindow(), bar, collapsing, toolbar, color);
    }

    /**
     * 复杂交互状态栏变化(CollapsingToolbarLayout + Toolbar)
     * @param window
     * @param bar
     * @param collapsing
     * @param toolbar
     * @param color
     */
    public static void setCollapsingToolbar(Window window, AppBarLayout bar, CollapsingToolbarLayout collapsing,
                                            Toolbar toolbar, int color) {
        COMPAT.translucentStatusBar(window, true);
        COMPAT.setCollapsingToolbar(window, bar, collapsing, toolbar, color);
    }

}
